﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using Tebbee.UI.Client.AdvertiseSer;
using Tebbee.UI.Client.StatisticSer;
using Tebbee.UI.Client.UtilitiesSer;

namespace Tebbee.UI.Client.Controllers.AdminApi
{
    public class StatisticController : ApiController
    {
        private StatisticSerClient db = new StatisticSerClient();
        private AdvertiseSerClient ad = new AdvertiseSerClient();
        private UtilitiesClient utilities = new UtilitiesClient();

        /// <summary>
        /// Get 10 newest user from system
        /// </summary>
        /// <returns></returns>
        public IEnumerable<User_Newsest_Result> GetNewestUser()
        {
            var tmp = db.Statistic_NewestUser();
            if (tmp == null) throw new HttpResponseException(new HttpResponseMessage
            {
                Content = new StringContent("Lỗi gì đó")
            });
            return tmp;
        }

        public IEnumerable<User_Statistic_Result> GetUserStatistic(int id = 0)
        {
            if (id == 0) id = DateTime.Now.Year;
            var tmp = db.Statistic_User(id);
            if (tmp == null) throw new HttpResponseException(new HttpResponseMessage
            {
                Content = new StringContent("Lối kỳ lạ")
            });
            return tmp;
        }

        public IEnumerable<Advertise_List_Result> GetAdvertisesStatistic()
        {
            var tmp = ad.ListAdvertising(0, 12, 0, "");
            Parallel.ForEach(tmp, item =>
            {
                var link = String.Empty;
                if (String.IsNullOrEmpty(item.Description))
                {
                    item.Description = "";
                }
                else
                {
                    link = item.Description.Replace("../", "");
                    var path = Path.Combine(HttpRuntime.AppDomainAppPath, link);
                    item.Description = utilities.ReadFile(path);
                }
            });
            return tmp;
        }

        public Statistic_HitsAds_Result GetHitsAds()
        {
            var tmp = db.Statistic_HitsAds();
            if (tmp == null) throw new HttpResponseException(new HttpResponseMessage
            {
                Content = new StringContent("Lỗi vô lý")
            });
            return tmp;
        }

        public Statistic_ViewLikeRate_Result GetViewLikeRate()
        {
            var tmp = db.Statistic_ViewLikeRate();
            if (tmp == null) throw new HttpResponseException(new HttpResponseMessage
            {
                Content = new StringContent("Lỗi vô lý")
            });
            return tmp;
        }


        public IEnumerable<Statistic_likeViewHits_Result> GetVLHStatistic(int id = 0)
        {
            if (id == 0) id = DateTime.Now.Year;
            var tmp = db.Statistic_LikeViewHits(id);
            if (tmp == null) throw new HttpResponseException(new HttpResponseMessage
            {
                Content = new StringContent("Lối kỳ lạ")
            });
            return tmp;
        }

        public Statistic_CountAdsHitUser_Result getCountAdsHitUser()
        {
            var tmp = db.Statistic_CountAdsHitUser();
            if (tmp == null) throw new HttpResponseException(new HttpResponseMessage
            {
                Content = new StringContent("Lỗi vô lý")
            });
            return tmp;
        }

        public IEnumerable<Statistic_Details_Result> getDetailsStatistic()
        {
            var queryString = HttpUtility.ParseQueryString(Request.RequestUri.Query);
            var from = queryString["FromDate"];
            var to = queryString["ToDate"];
            var tmp = db.Statistic_Details(Convert.ToDateTime(from), Convert.ToDateTime(to));
            if (tmp == null) throw new HttpResponseException(new HttpResponseMessage
            {
                Content = new StringContent("Lối kỳ lạ")
            });
            return tmp;
        }
    }
}
